
//
//  ZYEmoticonKeyBoard.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/20.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit

// cell重用标示
private let emoticonCollectionViewCellIdentifier = "emoticonCollectionViewCellIdentifier"



class ZYEmoticonKeyBoard: UIView {
    
    // 表情pageControl
    
    private lazy var emoticonPageControl: UIPageControl = {
       
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named:"compose_keyboard_dot_selected")!)
        pageControl.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        pageControl.numberOfPages = 5
        return pageControl
        
        
    }()
    

    // 表情视图
    private lazy var emoticonCollectionView:UICollectionView = {
       
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .Horizontal
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.backgroundColor = self.backgroundColor
        view.registerClass(ZYEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: emoticonCollectionViewCellIdentifier)
        view.pagingEnabled = true
        view.bounces = false
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        // 设置代理
        view.delegate = self
        view.dataSource = self
        
       return view

        
    }()
    
    
// 表情键盘toolbar
    private lazy var emoticonToolBar:ZYEmoticationToolbar = {
        
        let toolbar = ZYEmoticationToolbar(frame: CGRectZero)
        
        return toolbar
        
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
//        backgroundColor = UIColor.redColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
       // 添加子空间
       addSubview(emoticonToolBar)
       addSubview(emoticonCollectionView)
        addSubview(emoticonPageControl)
        
       // 设置约束
        emoticonToolBar.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(40)
            
        }
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.trailing.equalTo(self)
            make.leading.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
        }
        
        emoticonPageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
            make.height.equalTo(10)
        }
        
        
        emoticonToolBar.didSelectEmoticoBtnClosure = { [weak self] (type:ZYEmoticonToolBarButtonType) in
//            print("表情按钮")
            
            let indexPath:NSIndexPath
          
            switch type {
                
            case .Normal:

                indexPath = NSIndexPath(forItem: 0, inSection: 0)

            case .Emoji:
                
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
   
            case .Lxh:
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
        
            }
            
              self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            // 设置pageContol数据
            self?.setPageControlData(indexPath)
            
        }
        
    }
    
    // 设置获取pageControl数据源
    private func setPageControlData(indexPath: NSIndexPath) {
        emoticonPageControl.numberOfPages = ZYEmoticonTools.sharedEmoticonTools.allEmoticons[indexPath.section].count
        emoticonPageControl.currentPage = indexPath.item
        
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 获取collectionView的flowlayout
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = emoticonCollectionView.size
        
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
       
    }
    
}


// collectionView的代理源方法
extension ZYEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {

    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
          print(ZYEmoticonTools.sharedEmoticonTools.allEmoticons.count)
        return ZYEmoticonTools.sharedEmoticonTools.allEmoticons.count
      
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return ZYEmoticonTools.sharedEmoticonTools.allEmoticons[section].count
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(emoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! ZYEmoticonCollectionViewCell
        cell.emoticons = ZYEmoticonTools.sharedEmoticonTools.allEmoticons[indexPath.section][indexPath.row]
        return cell
    }
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            return  firstCell.x < secondCell.x
        }
        
        let contentOffSizeX = scrollView.contentOffset.x
        
        if cells.count == 2 {
            
            let firstCell = cells.first!
            let secondCell = cells.last!
            
            let firstCellContentOffSizeX = abs(firstCell.x - contentOffSizeX)
            let secondCellContentOffSizeX = abs(secondCell.x - contentOffSizeX)
            
            
            let indexPath:NSIndexPath
            if firstCellContentOffSizeX < secondCellContentOffSizeX {
                
                print("第1个显示多")
                indexPath = emoticonCollectionView.indexPathForCell(firstCell)!
            } else {
                
                print("第2个显示的多")
                indexPath = emoticonCollectionView.indexPathForCell(secondCell)!
                
            }
             let section = indexPath.section
            
            emoticonToolBar.selectEmoticonButtonWithSection(section)
            
            // 设置pageControl数据
            setPageControlData(indexPath)
        
        }
        
        
        
        
    }
    
}










